package com.xoa.service.knowledge.impl;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.knowledge.KnowledgeColumnMapper;
import com.xoa.dao.knowledge.KnowledgeDocfileMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.knowledge.KnowledgeColumn;
import com.xoa.model.knowledge.KnowledgeDocfile;
import com.xoa.service.knowledge.ColumnManageService;
import com.xoa.util.LimsJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ColumnManageServiceImpl implements ColumnManageService {

    @Autowired
    private KnowledgeColumnMapper knowledgeColumnMapper;

    @Autowired
    private KnowledgeDocfileMapper knowledgeDocfileMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;
    /**
     * @Author: ZhuYize
     * @Date: 2020/9/14 13:17
     * @Description: 添加栏目分类
     * @param: knowledgeColumn 添加的数据
     * @return: LimsJson
     */
    @Override
    public LimsJson insertKnowledgeType(KnowledgeColumn knowledgeColumn) {
        LimsJson limsJson = new LimsJson<>(1, "新增失败");
        limsJson.setFlag(1);
        // 判断新增记录有没有父类型，没有则为新增一级类型
        if (knowledgeColumn.isSysCode()) {
            knowledgeColumn.setParentColumnId(0);

//            // 查询所有一级父类的类型
//            List<String> columnTypeList = knowledgeColumnMapper.selectColumnTypeByParentColumnId(0);
//            if (columnTypeList.size() > 0) {
//                List<Integer> list = new ArrayList<>();
//                for (String str : columnTypeList) {
//                    list.add(Integer.valueOf(str));
//                }
//                Collections.sort(list);
//                columnType = String.valueOf(list.get(list.size() - 1) + 1);
//            }
        }
        int result = knowledgeColumnMapper.insertSelective(knowledgeColumn);
        if (result > 0) {
            limsJson.setCode(0);
            limsJson.setFlag(0);
            limsJson.setMsg("新增成功");
        }
        return limsJson;
    }


    /**
     * @Author: ZhuYize
     * @Date: 2020/9/14 15:01
     * @Description: 修改栏目类别
     * @param: knowledgeColumn 修改的数据
     * @return: LimsJson
     */
    @Override
    public LimsJson updateKnowledgeType(KnowledgeColumn knowledgeColumn) {
        LimsJson limsJson = new LimsJson<>(1, "修改失败");
        limsJson.setFlag(1);
        List<KnowledgeColumn> knowledgeColumnList = knowledgeColumnMapper.selectByParentColumnId(knowledgeColumn.getColumnId());
        if (knowledgeColumnList.size() > 0) {
            limsJson.setMsg("修改失败，栏目类型下存在子类型，不能修改");
        } else {
            String[] ids = {String.valueOf(knowledgeColumn.getColumnId())};
            // 通过栏目类型ids查询文件
            List<KnowledgeDocfile> knowledgeDocfileList = knowledgeDocfileMapper.selectByColumnIds(ids);
            if (knowledgeDocfileList.size() > 0) {
                limsJson.setMsg("修改失败，栏目类型下存在关联文档，不能修改");
            } else {
                if(knowledgeColumn.isSysCode()){
                    knowledgeColumn.setParentColumnId(0);
                }
                int result = knowledgeColumnMapper.updateByPrimaryKeySelective(knowledgeColumn);
                if (result > 0) {
                    limsJson.setFlag(0);
                    limsJson.setCode(0);
                    limsJson.setMsg("修改成功");
                }
            }
        }
        return limsJson;
    }


    /**
     * @Author: ZhuYize
     * @Date: 2020/9/14 18:32
     * @Description: 获取栏目类别详情
     * @param: columnId 栏目类别id
     * @return: LimsJson
     */
    @Override
    public LimsJson selectKnowledgeType(Integer columnId) {
        LimsJson limsJson = new LimsJson<>(1, "查询失败");
        limsJson.setFlag(1);
        if (columnId != null) {
            // 获取栏目类别详情
            KnowledgeColumn knowledgeColumn = knowledgeColumnMapper.selectByPrimaryKey(columnId);
            limsJson.setCode(0);
            limsJson.setFlag(0);
            if (knowledgeColumn != null) {
                if(knowledgeColumn.getParentColumnId()!=null&&knowledgeColumn.getParentColumnId()==0){  //说明是二级分类，他的上一级是sysCode表
                    if(knowledgeColumn.getColumnType()!=null){
                        SysCode sysCode = sysCodeMapper.getSingleCode("KNOWLEDGE_COLUMN_TYPE",knowledgeColumn.getColumnType());
                        if(sysCode!=null){
                            knowledgeColumn.setColumnTypeName(sysCode.getCodeName());
                            knowledgeColumn.setParentColumnIdName(sysCode.getCodeName());
                        }
                        knowledgeColumn.setSysCode(true);
                    }
                }else{
                    KnowledgeColumn parentKnow =  knowledgeColumnMapper.selectByPrimaryKey(knowledgeColumn.getParentColumnId());
                    if(parentKnow!=null){
                        knowledgeColumn.setParentColumnIdName(parentKnow.getColumnName());
                    }
                    SysCode sysCode = sysCodeMapper.getSingleCode("KNOWLEDGE_COLUMN_TYPE",knowledgeColumn.getColumnType());
                    if(sysCode!=null){
                        knowledgeColumn.setColumnTypeName(sysCode.getCodeName());
                    }
                    knowledgeColumn.setSysCode(false);
                }
                List<KnowledgeColumn> columnList = new ArrayList<>();
                columnList.add(knowledgeColumn);
                limsJson.setData(columnList);
                limsJson.setMsg("查询成功");
            } else {
                limsJson.setMsg("无数据");
            }
        } else {
            limsJson.setMsg("查询失败，程序错误，请联系开发人员");
        }
        return limsJson;
    }
}
